package com.o2opuzi.pub.controller

import java.util.HashMap;
import java.util.Map;

import javax.security.auth.login.LoginException;
import javax.servlet.http.Cookie;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.o2opuzi.lib.util.AESUtils;
import com.o2opuzi.lib.util.DigestUtils;
import com.o2opuzi.lib.util.ErrorMsgs;
import com.o2opuzi.lib.util.FieldErrorUtils;
import com.o2opuzi.lib.util.PasswordUtils;
import com.o2opuzi.lib.web.WebHelper;
import com.o2opuzi.pub.dao.RoleDao;
import com.o2opuzi.pub.model.Role;
import com.o2opuzi.pub.model.User;
import com.o2opuzi.pub.model.UserLoginLog;
import com.o2opuzi.pub.service.UserService;
import com.o2opuzi.pub.vo.LoginVo;
import com.o2opuzi.pub.vo.UserLoginVo;

@RestController
@RequestMapping("/admin/login")
class AdminLoginController {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleDao roleDao;
	
	/**
	 * 管理员登录
	 * @auther Y.hj
	 * @param loginVo
	 * @param result
	 * @param helper
	 * @return
	 */
	@PostMapping
	def login(@RequestBody @Valid LoginVo loginVo,BindingResult result,WebHelper helper){		
		try {
			if (result.hasErrors()) {
				return [
					code:0,
					messages:FieldErrorUtils.getErrors(result)
				];
			}
			def captcha = helper.sessionAttr("captcha");
			// 验证验证码
			if (!loginVo.captcha.toUpperCase().equals(captcha)) {
				return [
					code:0,
					messages:[captcha:"验证码不正确"]
				];
			}
			//用户
			User user = userService.login(
				new UserLoginVo(
					loginVo.account,
					PasswordUtils.encrypt(loginVo.password),
					helper.ipAddress,
					helper.clientType,
					true,
					UserLoginLog.LOGIN_TYPE_PASSWORD
				)
			);
			// 保持登录
			if (loginVo.keeplogin) {
				def jsonstr = JSON.toJSONString([
					account:user.account,
					password:loginVo.password
				]); 
				// 写入cookie
				helper.addCookie("sid", AESUtils.encrypt(jsonstr));
			}else{
				helper.removeCookie("sid");
			}
			//写入session
			Role role =  roleDao.findById(user.role_id);
			Set<String> menuSet = new HashSet<String>();
			if(!StringUtils.isEmpty(role.menu_auth)){
				String[] strs =  role.menu_auth.split(",");
				for (str in strs) {
					menuSet.add(str);
				}
			}
			helper.sessionAttr("adminLogin", [
				user:user,
				role:[
					name:role.name,
					menu:menuSet
				]
			]);
			// 删除session 中的验证码
			helper.removeSessionAttr("captcha");
			return [
				code:1,
				msg:"登录成功",
				return_url:"/admin/index"
			];
		} catch (LoginException e) {
			return [
				code:0,
				messages:[text:e.message]
			];
		}
		 
	}
	
	/**
	 * 退出登录
	 * @auther Y.hj
	 * @param helper
	 * @return
	 */
	@GetMapping("/logout")
	def logout(WebHelper helper){
		helper.removeCookie("sid");
		helper.removeSessionAttr("adminLogin");
		return [
			code:1,
			messages:[text:"退出成功,请重新登录"]
		];
	}
	
}
